package com.gnerv.battle.boot.module.base.business.impl;

import com.gnerv.battle.boot.module.base.business.OrgBusiness;
import com.gnerv.battle.boot.module.base.model.bo.OrgBO;
import com.gnerv.battle.boot.module.base.model.dto.OrgAccountDTO;
import com.gnerv.battle.boot.module.base.model.dto.OrgCreateDTO;
import com.gnerv.battle.boot.module.base.model.dto.OrgDTO;
import com.gnerv.battle.boot.module.base.model.dto.OrgTreeDTO;
import com.gnerv.battle.boot.module.base.model.dto.OrgUpdateDTO;
import com.gnerv.battle.boot.module.base.service.OrgService;
import com.gnerv.battle.boot.common.base.model.RestResult;
import com.gnerv.battle.boot.common.tools.UUIDTools;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@Service
@AllArgsConstructor
public class OrgBusinessImpl implements OrgBusiness {

    private final OrgService orgService;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> create(OrgCreateDTO orgCreateDTO) {
        OrgBO orgBO = orgCreateDTO.toOrgBO();
        orgBO.setUkId(UUIDTools.lower());
        orgService.create(orgBO);
        return RestResult.ok();
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> deleteByUkId(String ukId) {
        orgService.deleteByUkId(ukId);
        return RestResult.ok();
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> updateByUkId(OrgUpdateDTO orgUpdateDTO) {
        orgService.updateByUkId(orgUpdateDTO.toOrgBO());
        return RestResult.ok();
    }

    @Override
    public RestResult<OrgDTO> selectByUkId(String ukId) {
        return RestResult.ok(orgService.selectByUkId(ukId));
    }

    @Override
    public RestResult<List<OrgDTO>> listByName(String name) {
        return RestResult.ok(orgService.listByName(name));
    }

    @Override
    public RestResult<List<OrgDTO>> listByParentUkId(String parentUkId) {
        List<OrgDTO> orgList = orgService.listByParentUkId(parentUkId);
        return RestResult.ok(orgList);
    }

    @Override
    public RestResult<List<OrgTreeDTO>> listTreeByParentUkId(String parentUkId) {
        return RestResult.ok(orgService.listTreeByParentUkId(parentUkId));
    }

    @Override
    public RestResult<Void> bindAccount(OrgAccountDTO orgAccountDTO) {
        orgService.unbindAccount(orgAccountDTO.getOrgUkId());
        orgService.bindAccount(orgAccountDTO.getOrgUkId(), orgAccountDTO.getAccountUkIdList());
        return RestResult.ok();
    }

}
